//@include "json2.js"

function getOSDefaultPath() {
    if (File.fs === "Windows") {
        var desktopFolder = new Folder(Folder.desktop);
        var defaultPath = desktopFolder.fsName;
    } else if (File.fs === "Macintosh") {
        var defaultPath = "~/Desktop";
    } else {
        alert("只支持Win和Mac系统");
    }
    return defaultPath;
}

function getOsSolidus() {
    if (File.fs === "Windows") {
        var solidus = "\\";
    } else if (File.fs === "Macintosh") {
        var solidus = "/";
    } else {
        alert("只支持Win和Mac系统");
    }
    return solidus;
}

function formatTwoDigits(number) {
    return number < 10 ? '0' + number : number;
}

function formatThreeDigits(number) {
    if (number < 10) {
        return '00' + number;
    } else if (number < 100) {
        return '0' + number;
    } else {
        return number;
    }
}

function getCurrentDat() {
    // 获取当前日期和时间
    var currentDate = new Date();

    // 获取年、月、日、时、分、秒、毫秒
    var year = currentDate.getFullYear();
    var month = formatTwoDigits(currentDate.getMonth() + 1); // 月份是从0开始计数的，所以要加1
    var day = formatTwoDigits(currentDate.getDate());
    var hours = formatTwoDigits(currentDate.getHours());
    var minutes = formatTwoDigits(currentDate.getMinutes());
    var seconds = formatTwoDigits(currentDate.getSeconds());
    var milliseconds = formatThreeDigits(currentDate.getMilliseconds());

    var CDT = year + "." + month + "." + day;
    // var CDT = month + day + "_" +
    //     hours + minutes + "_" + seconds + "_" + milliseconds;
    // 打印当前日期和时间
    return CDT;
}

function getCurrentTime() {
    // 获取当前日期和时间
    var currentDate = new Date();

    // 获取年、月、日、时、分、秒、毫秒
    var year = currentDate.getFullYear();
    var month = formatTwoDigits(currentDate.getMonth() + 1); // 月份是从0开始计数的，所以要加1
    var day = formatTwoDigits(currentDate.getDate());
    var hours = formatTwoDigits(currentDate.getHours());
    var minutes = formatTwoDigits(currentDate.getMinutes());
    var seconds = formatTwoDigits(currentDate.getSeconds());
    var milliseconds = formatThreeDigits(currentDate.getMilliseconds());

    var CDT = month + day + "_" + hours + minutes;
    // var CDT = month + day + "_" +
    //     hours + minutes + "_" + seconds + "_" + milliseconds;
    // 打印当前日期和时间
    return CDT;
}

function getCurrentDocumentName() {
    if (app.documents.length > 0) {
        var currentDocument = app.activeDocument;
        var currentDocumentName = currentDocument.name;
        return currentDocumentName;
    } else {
        return null; // 如果没有打开的文档，返回null
    }
}

function removeFileExtension(fileName) {
    var lastDotIndex = fileName.lastIndexOf(".");
    if (lastDotIndex !== -1) {
        return fileName.substring(0, lastDotIndex);
    } else {
        return fileName; // 没有找到点，说明没有后缀
    }
}

function getCapitalizeWords(inputString) {
    if (inputString === undefined || inputString === null) {
        return "Input string is undefined or null.";
    }

    // 使用传统循环将单词转为大写
    var words = inputString.split(/\s+/); // 根据空格分割单词
    for (var i = 0; i < words.length; i++) {
        words[i] = words[i].toUpperCase();
    }

    return words.join(" "); // 将单词合并为字符串
}

function getPath() {
    // 获取当前脚本文件的路径
    var currentScriptPath = $.fileName;

    // 在某些系统中，路径分隔符可能是反斜杠
    // 这里统一将路径分隔符替换为正斜杠
    var currentScriptPathNormalized = currentScriptPath.replace(/\\/g, '/');

    // 通过正斜杠分割路径
    var pathSegments = currentScriptPathNormalized.split('/');

    // 去掉文件夹名部分，保留路径的父目录
    pathSegments.pop(); // 移除最后一部分，即文件夹名
    var scriptFolderPath = pathSegments.join('/');

    // 再次去掉文件夹名部分，获取上一级目录
    pathSegments.pop(); // 移除再最后一部分

    // 获取上一级目录
    var parentDirectory = pathSegments.join('/');

    // 获取上一级目录下的 utils 文件夹路径
    var utilsFolderPath = parentDirectory + '/utils';
    var commonFolderPath = parentDirectory + '/common';
    var libFolderPath = parentDirectory + '/lib';

    return {
        // Directory: Directory,
        parentDirectory: parentDirectory,
        utilsFolderPath: utilsFolderPath,
        libFolderPath: libFolderPath,
        commonFolderPath: commonFolderPath,
        scriptFolderPath: scriptFolderPath
    }
}

// 将中文字符转换为 URL 编码
function encodeChineseToURL(chineseString) {
    return chineseString.replace(/[\u4E00-\u9FA5]/g, function(chineseChar) {
        return encodeURIComponent(chineseChar);
    });
}

function createFolder(folderName, fullPath) {

    fullPath = fullPath || getOSDefaultPath()
    folderName = folderName || "newFolder"

    // 创建文件夹
    var folderPath = Folder(fullPath + getOsSolidus() + folderName);
    // 检查文件夹是否存在，如果不存在则创建
    if (!folderPath.exists) {
        folderPath.create();
        //   alert("已创建文件夹")
    } else {
        // alert(fullPath)
        //  alert("已存在文件夹")

    }
    return fullPath + getOsSolidus() + folderName

}